<template>
  <div>
    <van-nav-bar
      title="我的帖子"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 我的帖子列表 -->
    <MyBBSList :list="list" :total="total" :finished="finished" :loading="loading" @onLoading="onLoad" @toDetail="goToDetail"></MyBBSList>
  </div>
</template>
<script>
import { NavBar} from "vant";
import MyBBSList from '@/components/MyInfoModuleComponents/BBSList.vue'
import {getUserBBSList,getUserBBSListNum,getOneUser} from "@/api/user.js";
export default {
  data() {
    return {
      //用户数据
      user: {},
      //帖子列表项
      list: [],
      //最大总数
      total:999,
      //加载状态
      loading: true,
      //是否已经加载完毕
      finished:false,
      //当前帖子数量
      nowNum:0
    };
  },
  methods: {
    //返回按钮
    onClickLeft() {
      history.back();
    },
    //获取帖子列表
    onLoad(){
      this.getBSSlistItem();
    },
    //懒加载帖子
    async getBSSlistItem(){
      this.loading = true
      const params = {
        username:this.user.username,
        limit:this.limit,
        nowNum:this.nowNum,
      }
      let res = await getUserBBSList(params)
      if(res.success){
        this.list.push(...res.BBS_item_List);
        this.nowNum = this.list.length
        this.loading = false;
        if(this.list.length >= this.total){
          this.finished = true;
        }
      }
    },
    //前往帖子详情页
    goToDetail(item){
      const params = {post_id:item._id,...item,...this.user}
      this.$router.push({
          name: "postindex",
          params,
      });
    },
  },
  components: {
     MyBBSList,
    [NavBar.name]: NavBar,
  },
  async created() {
    //console.log(this.$route.params);
    let user = {}
    if(!Object.keys(this.$route.params).length){
      user = await getOneUser(sessionStorage.getItem("username"));
      delete user.password;
    }
    this.user = Object.assign({},this.$route.params,user);
    let that = this
    //获取当前用户发布帖子的数量，用于进行懒加载。
    await getUserBBSListNum(this.user.username).then(res =>{
       that.total = res.total;
    })
    this.getBSSlistItem()
  },
};
</script>
<style lang="less" scoped>
</style>